package com.zdong.hintview;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;

/**
 * Created by user on 2015/10/14.
 */
public class Myaccessibility extends AccessibilityService {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.e("TAG", "onAccessibilityEvent" + event.toString());

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("TAG","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected boolean onKeyEvent(KeyEvent event) {
        Log.e("TAG","onKeyEvent"+event.toString());
        return super.onKeyEvent(event);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG","onUnbind");
        sendUesrBroadcast(false);
        return super.onUnbind(intent);
    }

    @Override
    protected void onServiceConnected() {
        Log.e("TAG","onServiceConnected");
        this.sendUesrBroadcast(true);
        setServiceInfo(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        super.onServiceConnected();
    }
    private void setServiceInfo(int feedbackType) {
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        // We are interested in all types of accessibility events.
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        // We want to provide specific type of feedback.
        info.feedbackType = feedbackType;
        // We want to receive events in a certain interval.
        info.notificationTimeout = 100;
        // We want to receive accessibility events only from certain packages.
        info.packageNames = new String[]{this.getPackageName()};
        setServiceInfo(info);
    }

    private void sendUesrBroadcast(boolean open) {
        Intent intent=new Intent("android.intent.action.USER_ZDONG");
        intent.putExtra("isopen",open);
        sendBroadcast(intent);
    }

    @Override
    public void onInterrupt() {
        Log.e("TAG","onInterrupt");
    }

    @Override
    public void onDestroy() {
        Log.e("TAG", "onDestroy");
//        sendUesrBroadcast(false);
        super.onDestroy();
    }
}
